var salt = sessionStorage.getItem('usersalt');

layui.use(['form', 'layer', 'laypage', 'laytpl'], function() {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		laytpl = layui.laytpl,
		laypage = layui.laypage;

	/*验证登录*/
	if(!sessionStorage.getItem('username')) {
		layer.msg('请先登录');
		setTimeout("top.location.href='../../../index.html'", 1000);
	}
	
	//渲染ul的高度
	function setUlHeight(){
		var bodyHeight = $("body").height();
		var tabHeight = $(".topsty").height();
		$("form").height(bodyHeight - tabHeight);
		
		var searchHeight = $(".searchDiv").height();
		var pageHeight = $(".pagediv").height();
		$("ul").height($("form").height() - searchHeight - pageHeight - 20);
	}
	setUlHeight();
	
	/*取出所有的岗位*/
	var post;
	function getParentPost() {
		$.ajax({
			url: url + 'post/getParentPost',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {
			},
			success: function(data) {
				if(data.code == 0) {
					post = data.data;

					//渲染岗位html
					var postType = $("#postType").html();
					laytpl(postType).render(post, function(html){
					  	$("#postTypeView").html(html);
					  	
					  	//html点击事件
					  	$("body").click(function() {
							$("#postSelect").hide();
						})
						$('#postInput').click(function() {
							$("#postSelect").show();
							return false;
						});
					});
				}
			},
			error: function(error) {
				console.log("获取失败");
			}
		});
	}
	getParentPost();
	
	//选择岗位业务
	window.choosePostType = function(id, name){
		$("#postTypeId").val(id);
		$("#postTypeName").val(name);
		$(".postType").removeClass("layui-this");
		$("#postType" + id).addClass("layui-this");
	};
	
	/*取出课程分类*/
	var courseTypes;
	function getCourseType() {
		$.ajax({
			url: url + 'course/getCourseType',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {
			},
			success: function(data) {
				if(data.code == 0) {
					courseTypes = data.data;

					//渲染课程分类
					var courseType = $("#courseType").html();
					laytpl(courseType).render(courseTypes, function(html){
					  	$("#courseTypeView").html(html);
					  	
					  	//html点击事件
					  	$("body").click(function() {
							$("#courseSelect").hide();
						})
						$('#courseInput').click(function() {
							$("#courseSelect").show();
							return false;
						});
					});
				}
			},
			error: function(error) {
				console.log("获取失败");
			}
		});
	}
	getCourseType();
	
	//选择课程分类
	window.chooseCourseType = function(id, name){
		$("#courseTypeId").val(id);
		$("#courseTypeName").val(name);
		$(".courseType").removeClass("layui-this");
		$("#courseType" + id).addClass("layui-this");
	};
	
	//筛选
	$("#selList,#search").on("click", function(){
		getCourseList();
	});


	//获取培训课程列表
	var pageObj = {page : 1,limit : 10,total : -1};
	var record = {url : "",list : []};
	function getCourseList(){
		var inptext = $('#inptext').val();
		var box = /\s/g;
		var searchText = inptext.replace(box, '');
		var postType = $("#postTypeId").val();
		var courseType = $("#courseTypeId").val();
		$.ajax({
			url: url + "course/getPage",
			dataType: "json",
			type: "post",
			async: true,
			data: {
				Token: salt,
				page: pageObj.page,
				limit: pageObj.limit,
				name: searchText,
				postTypeId: postType,
				courseTypeId: courseType
			},
			success: function(data) {
				if (pageObj.total == -1) {//页面初始化
					pageObj.total = data.count;
					initPaging();
				}
				
				record.url = url;
				record.list = data.data;
				//渲染课程列表html
				var images = $("#images").html();
				laytpl(images).render(record, function(html){
				  	$("#imagesView").html(html);
				  	
				  	//图片高度
				  	$(".ulimg li img").height($(".ulimg li img").width() * 0.66);
				});
			},
			error: function(error) {
				console.log("获取失败");
			}
		});
	}
	getCourseList();
	
	//页码
	function initPaging(){
	  	laypage.render({
	    	elem: 'paging',
	    	count: pageObj.total,
	    	prev: "<i class=\"layui-icon\"></i>",
	    	next: "<i class=\"layui-icon\"></i>",
	    	layout: ['prev', 'page', 'next', 'refresh', 'skip', 'count', 'limit'],
	    	jump: function(obj){
	      		pageObj.page = obj.curr;
	    		pageObj.limit = obj.limit;
	      		getCourseList();
	    	}
	  	});
	}
	
	var selCourseArr = new Array();
	window.selCourse = function(index, courseId){
		var idx = selCourseArr.indexOf(courseId);
		if (idx > -1) {
			selCourseArr.splice(idx, 1);
			$("#course" + index).removeClass("layui-form-checked");
			return;
		}
		selCourseArr.push(courseId);
		$("#course" + index).addClass("layui-form-checked");
	};
	
	//编辑
	$(".edit_btn").click(function(){
		if (selCourseArr.length > 1) {
			layer.msg("只能选择一条数据");
			return;
		}
		if (selCourseArr.length == 0) {
			layer.msg("请选择一条数据");
			return;
		}
		
		location.href = "../course-add/course-add.html?id=" + selCourseArr[0];
	});
	
	//删除
	$(".del_btn").click(function(){
		if (selCourseArr.length == 0) {
			layer.msg("请选择需要删除的数据");
			return;
		}
		
		layer.confirm('确定删除选中的数据吗？', {
				icon: 3,
				title: '提示信息'
			}, function(index) {
				$.ajax({
					url: url + 'course/del',
					dataType: 'json',
					async: false,
					type: "POST",
					data: {
						Token: salt,
						id: selCourseArr.toString()
					},
					success: function(data) {
						if(data.code == 0) {
							layer.msg('删除成功');
						} else {
							layer.msg(data.msg);
						}

					},
					error: function(error) {
						console.log("获取失败");
					}
				});

				getCourseList();
				layer.close(index);
			})
	});
	
	//详情
	window.showDetail = function(id){
		location.href = "../course-detail/course-detail.html?id=" + id;
	};
	
	//下载
    $("body").on("click",".downLoad",function () {
        var fileId = $(this).attr("fileId");
        window.open(url + '/attachment/seekFile/' + fileId);
    })
})
